package cn.doitedu.eagle.ruleutils;

import cn.doitedu.eagle.beans.Keyed;
import cn.doitedu.eagle.beans.LogBean;

import java.lang.reflect.Field;
import java.util.Map;

public class KeyExtractor {


    public static String extractKes(LogBean bean,String keys) throws NoSuchFieldException, IllegalAccessException {
        String[] split = keys.split(",");
        StringBuffer sb = new StringBuffer();
        for (String k : split) {
            if(!k.contains(".")) {  // 普通字段抽取
                Field declaredField = bean.getClass().getDeclaredField(k);
                declaredField.setAccessible(true);
                String value = (String) declaredField.get(bean);
                sb.append(value);
            }else{  // properties字段抽取
                String[] split1 = k.split(".");
                Field properties = bean.getClass().getDeclaredField("properties");
                properties.setAccessible(true);
                Map<String,String> props = (Map<String, String>) properties.get(bean);
                sb.append(props.get(split1[1]));
            }
        }
        return sb.toString();
    }



}
